package com.enen.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.enen.controller.utils.JwtUtil;
import com.enen.controller.utils.R;
import com.enen.domain.Old;
import com.enen.domain.ProfileResult;
import com.enen.service.OldService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/olds")
public class OldController {
    @Autowired
    private OldService oldService;

    @Autowired
    private JwtUtil jwtUtil;
    /**
     * 添加老人
     * @param old
     * @return
     * @throws IOException
     */
    @PostMapping
    public R save(@RequestBody Old old) throws IOException {
        //模拟异常
        //if(old.getOldName().equals("123")) throw new IOException();
        boolean flag = oldService.save(old);
        return new R(flag,flag ? "添加成功~":"添加失败");
    }

    /**
     * 删除老人
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R deleteByid(@PathVariable int id){
        return new R(oldService.removeById(id));
    }

    /**
     * 修改老人信息
     * @param old
     * @return
     */
    @PutMapping
    public R update(@RequestBody Old old){
        return new R(oldService.updateById(old));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getById(@PathVariable int id){
        return new R(true,oldService.getById(id));
    }

    /**
     * 分页查询
     * @param current
     * @param pages
     * @param old
     * @return
     */
    @GetMapping("{current}/{pages}")
    public R getByPage(@PathVariable int current,@PathVariable int pages,Old old){
        IPage page = oldService.selectByPage(current, pages,old);
        if(page.getCurrent()>page.getPages()){
            IPage page1 = oldService.selectByPage((int)page.getPages(), pages,old);
            System.out.println(page);
            return new R(true,page1);
        }else{
            System.out.println(page);
            return new R(true,page);
        }
    }

    /**
     * 登录获取token
     */
    @PostMapping("/login")
    public R log(@RequestBody Map<String,String> loginMap) {
        String phone = loginMap.get("phone");
        String password = loginMap.get("password");
        Old old = oldService.selectByPhone(phone);
        if(old == null || !old.getPassword().equals(password)){
            return new R ("登录失败");
        }else{
            System.out.println("111");
            Map<String,Object> map = new HashMap<>();
            map.put("oldPhone",old.getOldPhone());
            String id = old.getOldId()+"";
           Object token = jwtUtil.createJwt(id,old.getOldName(),map);
            return new R(true,token,"登陆成功");
        }
    }

    /**
     * 获取用户数据
     */
    @PostMapping("/file")
    public R file(HttpServletRequest request) throws IOException {
        String author = request.getHeader("Author");
        if(StringUtils.isEmpty(author)){
            throw new IOException();
        }
        String token = author.replace("Bearer ","");

        Claims claims =  jwtUtil.parseJwt(token);

        String id = claims.getId();
        Old old = oldService.getById(id);
        ProfileResult profileResult = new ProfileResult(old);
        return new R(true,profileResult);

    }

}
